package com.liglei;

import cn.hutool.extra.ftp.SimpleFtpServer;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.impl.DefaultConnectionConfig;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.ArrayList;
import java.util.List;

/**
 * FTP 服务端
 * @Author:
 * @CreateTime: 2024-07-19  17:18
 * @Description:
 * @Version: 1.0
 */
public class FTPService {

    private static final Log log = LogFactory.get(FTPService.class);

    public static void main(String[] args) {
        initFtp();
        server.start();
        // FtpLetListener.setReceiveController(receiveController);
        log.info("FTP开启!");
    }

    // FTP服务
    private static SimpleFtpServer server;

    private static String path = "/volume1/homes/video";
//    private String path = "E:\\桌面\\upload";

    public static void initFtp() {
        //增加SSL安全配置  --暂时不需要

        //配置自定义用户
        BaseUser lekang = new BaseUser();
        lekang.setName("video");
        lekang.setPassword("video123.");
        lekang.setHomeDirectory(path);
        lekang.setMaxIdleTime(5000);
        //设置用户权限
        List<Authority> authorities = new ArrayList<Authority>();
        authorities.add(new WritePermission());
        lekang.setAuthorities(authorities);
        // 设置连接配置
        // 启用匿名登录 anonymousLoginEnabled
        // 登录失败延迟 loginFailureDelay
        // 最大登录数 maxLogins
        // 最大匿名登录数 maxAnonymousLogins
        // 最大登录失败次数 maxLoginFailures
        // 最大线程 maxThreads
        DefaultConnectionConfig config = new DefaultConnectionConfig(false, 500, Short.MAX_VALUE, 10, 3, 0);
//        server = SimpleFtpServer.create().addUser(lekang).setConnectionConfig(config).setPort(25).addFtplet("folder", new DefaultFtplet());
        server = SimpleFtpServer.create().addUser(lekang).setConnectionConfig(config).setPort(25);
    }
}
